package router

import (
	"github.com/gin-gonic/gin"
	"todo-gin-server/internal/infrastructure/auth"
	"todo-gin-server/internal/infrastructure/persistence"
	"todo-gin-server/internal/interfaces/api"
	"todo-gin-server/internal/interfaces/middleware"
)

func SetupRouter() *gin.Engine {
	// 初始化数据库
	persistence.InitDatabase()
	
	// 初始化Casbin
	auth.InitCasbin(persistence.DB)
	
	// 创建控制器
	authController := api.NewAuthController()
	todoController := api.NewTodoController()
	
	r := gin.Default()
	r.Use(gin.Logger())
	
	// 公开路由
	authGroup := r.Group("/api/auth")
	{
		authGroup.POST("/register", authController.Register)
		authGroup.POST("/login", authController.Login)
	}
	
	// 需要认证的路由
	apiGroup := r.Group("/api")
	apiGroup.Use(middleware.JWTAuthMiddleware())
	apiGroup.Use(middleware.CasbinAuthMiddleware())
	{
		// 待办事项路由
		todoGroup := apiGroup.Group("/todos")
		{
			todoGroup.GET("", todoController.GetAllTodos)
			todoGroup.POST("", todoController.CreateTodo)
			todoGroup.GET("/:id", todoController.GetTodoByID)
			todoGroup.PUT("/:id", todoController.UpdateTodo)
			todoGroup.DELETE("/:id", todoController.DeleteTodo)
		}
	}
	
	return r
}